package com.chunfen.tomcat.designpattern;

import java.util.Random;

public class HasQuarterState extends State {

    Random randomWinnerState = new Random(System.currentTimeMillis());

    HasQuarterState(GumballMachine gumballMachine){
        this.gumballMachine = gumballMachine;
    }

    @Override
    protected void insertQuarter() {
        System.out.println("You can't insert another quarter");
    }

    @Override
    protected void ejectQuarter() {
        System.out.println("Quarter returned");
        gumballMachine.setState(gumballMachine.getNoQuarterState());
    }

    @Override
    protected void turnCrank() {
        System.out.println("You turned...");
//        gumballMachine.setState(gumballMachine.getSoldState());
        int winner = randomWinnerState.nextInt(10);
        if((winner == 0) && gumballMachine.getCount() > 1){
            gumballMachine.setState(gumballMachine.getWinnerState());
        } else {
            gumballMachine.setState(gumballMachine.getSoldState());
        }
    }

    @Override
    protected void dispense() {
        System.out.println("No gumball dispensed");
    }
}
